package com.yh.correct.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.common.utils.StringUtils;
import com.yh.common.utils.codec.MD5Utils;
import com.yh.correct.dao.IJudicialDeptmentMapper;
import com.yh.correct.dao.ISysPermissionCustomMapper;
import com.yh.correct.dao.ISysRoleMapper;
import com.yh.correct.dao.ISysUserMapper;
import com.yh.correct.dao.ISysUserRoleMapper;
import com.yh.correct.model.ActiveUser;
import com.yh.correct.model.JudicialDeptment;
import com.yh.correct.model.SysPermission;
import com.yh.correct.model.SysRole;
import com.yh.correct.model.SysUser;
import com.yh.correct.model.SysUserRole;
import com.yh.correct.model.vo.CustomException;
import com.yh.correct.service.ISysUserService;
/**
 * 系统用户service层接口实现类
 * @author Administrator
 *
 */
@Service
public class SysUserServiceImpl implements ISysUserService {
	/**
	 * 使用logger日志
	 */
    private static Logger logger = LogManager.getLogger(SysUserServiceImpl.class);
    /**
	 * 用户dao层接口
	 */
	@Autowired
	private ISysUserMapper userMapper;
	/**
	 * 用户角色dao层接口
	 */
	@Autowired
	private ISysUserRoleMapper userroleMapper;
	/**
	 *	用户权限自定义dao层接口
	 */
	@Autowired
	private ISysPermissionCustomMapper permissionCustomMaper;
	/**
	 * 司法部门dao层接口
	 */
	@Autowired
	private IJudicialDeptmentMapper deptmentMapper;
	/**
	 * 角色dao层接口
	 */
	@Autowired
	private ISysRoleMapper roleMapper;
	/**
	 * 获取用户信息。
	 * 1.用户表。2.用户角色表。3.角色表。4.所属单位表
	 */
	@Override
	public Map<String, Object> getSysUser(SysUser user) throws Exception {
		// TODO Auto-generated method stub
		Map<String,Object> userMap=new HashMap<String,Object>();
		SysUser sysuser=userMapper.getSysUser(user);//获取用户对象
		userMap.put("user", sysuser);
		int roleid=userroleMapper.findSysUserRoleByUserId(user.getId()).getRoleid();//获取用户对应的角色id
		SysRole role=new SysRole();
		role.setId(roleid);
		userMap.put("userrole",roleMapper.getSysRole(role));//用户对应的角色对象
		JudicialDeptment jd=new JudicialDeptment();
		jd.setId(sysuser.getJid());
		userMap.put("dept",deptmentMapper.getJudicialDeptment(jd));//获取所属单位对象
		return userMap;
	}

	/**
	 * 添加用户信息。
	 * 1.用户表。2.用户角色表
	 */
	@Override
	@Transactional
	public int insertSysUser(SysUser user,Integer roleid) throws Exception {
		// TODO Auto-generated method stub
		String salt=StringUtils.getRandomNumbersAndLetters(4);
		SimpleHash simpleHash = new SimpleHash("md5", user.getPassword(), salt, 1);//加密
		user.setPassword(simpleHash.toString());
		user.setSalt(salt);
		int i=userMapper.insertSysUser(user);//用户表添加
		SysUserRole  userrole=new SysUserRole();
		userrole.setUserid(user.getId());
		userrole.setRoleid(roleid);
		i+=userroleMapper.insertSysUserRole(userrole);//用户角色表添加
		return i;
	}

	/**
	 * 删除用户信息。
	 * 1.用户表。2.用户角色表
	 */
	@Override
	@Transactional
	public int deleteSysUser(SysUser user) throws Exception {
		// TODO Auto-generated method stub
		int i=userMapper.deleteSysUser(user);//用户表删除
		i+=userroleMapper.deleteByUserId(user.getId());//用户角色表删除
		return i;
	}

	/**
	 * 更新用户信息
	 *  1.用户表。2.用户角色表
	 */
	@Override
	@Transactional
	public int updateSysUser(SysUser user,SysUserRole userrole) throws Exception {
		// TODO Auto-generated method stub
		int i=userMapper.updateSysUser(user);//更新用户信息(除了密码)
		i+=userroleMapper.updateByUserId(userrole);//更新用户角色关系
		return i;
	}

	/**
	 * 动态条件查询用户分页
	 */
	@Override
	public PageInfo<Object> findListByCondition(Map<String, Object> condition) throws Exception {
		// TODO Auto-generated method stub
		PageHelper.startPage((int)condition.get("pageNum"), (int)condition.get("pageSize"), true);
		PageInfo<Object> pageInfo=new PageInfo<Object>(userMapper.findListByCondition(condition));
		return pageInfo;
	}

	/**
	 * 通过用户名查询找用户
	 */
	@Override
	public SysUser findSysUserByUsername(String username) throws Exception {
		// TODO Auto-generated method stub
		return userMapper.findSysUserByName(username) ;
	}

	/**
	 * 通过userid查询用户菜单列表
	 */
	@Override
	public List<SysPermission> findMenuListById(int id) throws Exception {
		// TODO Auto-generated method stub
		return permissionCustomMaper.findMenuListByUserId(id);
	}

	/**
	 * 通过userid查询用户权限列表
	 */
	@Override
	public List<SysPermission> findPermissionListById(int id) throws Exception {
		// TODO Auto-generated method stub
		return permissionCustomMaper.findPermissionListByUserId(id);
	}

	/**
	 * 通过userid修改用户的密码
	 */
	@Override
	@Transactional
	public int updateSysUserPwd(SysUser user) throws Exception {
		// TODO Auto-generated method stub
		SysUser sysUser=userMapper.getSysUser(user);//获取用户,取到盐值
		Md5Hash md5Hash=new Md5Hash(user.getPassword(),sysUser.getSalt(),1);//加密
		return userMapper.updateSysUserPwd(user.getId(), md5Hash.toString());//执行更新
	}

}
